<?php
namespace app\restaurant\model;

use app\BaseModel;
use app\dynamic\model\CardUser;
use app\dynamic\model\UserInfo;
use think\facade\Db;

class Store extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_store';


    protected $append = [

        'staff',

        'foods_out_status',

        'foods_house_status'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-10-24 16:13
     * @功能说明:外卖状态
     */
    public function getFoodsOutStatusAttr($value,$data){

        $status = 0;

        if(isset($data['out_work'])&&isset($data['out_work_end'])&&isset($data['out_work_start'])&&isset($data['take_out_status'])&&isset($data['out_all_day'])&&isset($data['no_out_work'])&&isset($data['out_no_work_start'])&&isset($data['out_no_work_end'])){

            if($data['take_out_status']==1){

                if($data['out_all_day']==1){

                    $status = 1;

                }else{
                    //查看今日星期几
                    $week = date('w');
                    //如果今天是周末
                    if(in_array($week,[6,0])&&$data['no_out_work']==1){

                        $start_time = strtotime($data['out_no_work_start']);

                        $end_time   = strtotime($data['out_no_work_end']);

                        if(time()>$start_time&&time()<$end_time){

                            $status = 1;
                        }

                    }elseif($data['out_work']==1){

                        $start_time = strtotime($data['out_work_start']);

                        $end_time   = strtotime($data['out_work_end']);

                        if(time()>$start_time&&time()<$end_time){

                            $status = 1;
                        }

                    }
                    //查看昨天数据 主要针对结束时间有到次日情况
                    $yes_status = $this->getYesOutStatus($data);

                    if($yes_status==1){

                        $status = 1;
                    }

                }

            }

        }

        return $status;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-11 10:03
     * @功能说明:获取昨日堂食的时间
     */
    public function getYesOutStatus($data){
        //查看昨日星期几
        $week = date('w',time()-86400);

        $status = 0;
        //如果昨日是周末
        if(in_array($week,[6,0])&&$data['no_out_work']==1){
            //如果结束时间小于等于开始时间 说明结束时间是到次日
            if($data['out_no_work_end']<=$data['out_no_work_start']){

                $end_time   = strtotime($data['out_no_work_end']);

                if(time()<$end_time){

                    $status = 1;

                }

            }

        }

        if($data['out_work']==1&&!in_array($week,[6,0])){

            if($data['out_work_end']<=$data['out_work_start']){

                $end_time = strtotime($data['out_work_end']);

                if(time()<$end_time){

                    $status = 1;
                }

            }

        }

        return $status;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-24 16:13
     * @功能说明:外卖状态
     */
    public function getFoodsHouseStatusAttr($value,$data){

        $status = 0;

        if(isset($data['status'])&&isset($data['all_day'])&&isset($data['no_work_day'])&&isset($data['no_work_start'])&&isset($data['no_work_end'])&&isset($data['work_day'])&&isset($data['work_start'])&&isset($data['work_end'])) {

            if($data['status']==1){

                if($data['all_day']==1){

                    $status = 1;

                }else{
                    //查看今日星期几
                    $week = date('w');
                    //如果今天是周末
                    if(in_array($week,[6,0])&&$data['no_work_day']==1){

                        $start_time = strtotime($data['no_work_start']);

                        $end_time   = strtotime($data['no_work_end']);

                        $end_time   = $data['no_work_start']>=$data['no_work_end']?$end_time+86400:$end_time;

                        if(time()>$start_time&&time()<$end_time){

                            $status = 1;
                        }

                    }

                    if($data['work_day']==1&&!in_array($week,[6,0])){

                        $start_time = strtotime($data['work_start']);

                        $end_time   = strtotime($data['work_end']);

                        $end_time   = $data['work_start']>=$data['work_end']?$end_time+86400:$end_time;

                        if(time()>$start_time&&time()<$end_time){

                            $status = 1;
                        }

                    }
                    //查看昨天数据 主要针对结束时间有到次日情况
                    $yes_status = $this->getYesHouseStatus($data);

                    if($yes_status==1){

                        $status = 1;
                    }

                }

            }

        }

        return $status;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-11 10:03
     * @功能说明:获取昨日堂食的时间
     */
    public function getYesHouseStatus($data){
        //查看昨日星期几
        $week = date('w',time()-86400);

        $status = 0;
        //如果昨日是周末
        if(in_array($week,[6,0])&&$data['no_work_day']==1){
            //如果结束时间小于等于开始时间 说明结束时间是到次日
            if($data['no_work_end']<=$data['no_work_start']){

                $end_time   = strtotime($data['no_work_end']);

                if(time()<$end_time){

                    $status = 1;

                }

            }

        }

        if($data['work_day']==1&&!in_array($week,[6,0])){

            //如果结束时间小于等于开始时间 说明结束时间是到次日
            if($data['work_end']<=$data['work_start']){

                $end_time = strtotime($data['work_end']);

                if(time()<$end_time){

                    $status = 1;
                }

            }

        }

        return $status;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-12 11:06
     * @功能说明:
     */
    public function getStaffAttr($value,$data){

        if(!empty($data['id'])){

            $staff_model = new StoreStaff();

            $staff_id    = $staff_model->where(['store_id'=>$data['id'],'role'=>1])->value('staff_id');

            return $staff_id;
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:26
     * @功能说明:
     */
    public function getImgAttr($value){

        return !empty($value)?explode(',',$value):'';

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:26
     * @功能说明:
     */
    public function getArchivesAttr($value){

        return !empty($value)?explode(',',$value):'';

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:26
     * @功能说明:
     */
    public function getServiceAttr($value){

        return !empty($value)?explode(',',$value):'';

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29    11:26
     * @功能说明:
     */
    public function getCityCodeAttr($value){

        return !empty($value)?explode(',',$value):'';

    }






    /**
     * @author chenniang
     * @DataTime: 2020-10-21 15:21
     * @功能说明:保留两位小数
     */
    public function getDistanceAttr($value){

        if(!empty($value)){

            if($value>1000){

                $value = $value/1000;

                $value =  round($value,2);

                $value = $value.'km';
            }else{

                $value =  round($value,2);

                $value = $value.'m';

            }

            return $value;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:门店添加
     */
    public function storeAdd($data){

        $data = $this->changeData($data);

        $data['create_time'] = time();

        $data['update_time'] = time();

        $staff = $data['staff'];

        unset($data['staff']);

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($staff,$id,$data['uniacid']);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-12 09:41
     * @功能说明:
     */
    public function updateSome($staff,$id,$uniacid){

        $store_staff_model = new StoreStaff();

        $store_staff_model->where(['store_id'=>$id])->update(['role'=>0]);

        if(!empty($staff)){

            $staff_info = $store_staff_model->where(['store_id'=>$id,'staff_id'=>$staff])->find();

            if(!empty($staff_info)){

                $store_staff_model->where(['id'=>$staff_info->id])->update(['role'=>1]);

            }else{

                $insert = [

                    'uniacid' => $uniacid,

                    'staff_id'=> $staff,

                    'store_id'=> $id,

                    'role'    => 1,

                ];

                $store_staff_model->staffAdd($insert);
            }

        }

        return true;



    }




    /**
     * @param $data
     * @功能说明:转换数据
     * @author chenniang
     * @DataTime: 2020-10-09 14:24
     */
    public function changeData($data){

        $data['img'] = !empty($data['img'])?implode(',',$data['img']):'';

        $data['service'] = !empty($data['service'])?implode(',',$data['service']):'';

        $data['archives'] = !empty($data['archives'])?implode(',',$data['archives']):'';

        $data['city_code'] = !empty($data['city_code'])?implode(',',$data['city_code']):'';

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:门店编辑
     */
    public function storeUpdate($dis,$data){

        $data = $this->changeData($data);

        $staff = $data['staff'];

        unset($data['staff']);

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        $this->updateSome($staff,$dis['id'],$dis['uniacid']);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function storeList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        $printer_model = new Printer();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['staff_name'] = lbUserName($v['staff']);

                $v['printer_num']= $printer_model->where(['store_id'=>$v['id'],'status'=>1])->count();

            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-03 13:50
     * @功能说明:扫码进来的时候桌号与用户绑定
     */
    public function selectTable($user_id,$table_id,$user_info){

        $table_model = new Table();

        $dis = [

            'id' => $table_id,

            'status' => 1
        ];

        $table = $table_model->dataInfo($dis);

        if(empty($table)){

            return ['code'=>500,'msg'=>'桌号已下架'];
        }

        $update = [

            'table_id' => $table_id,

            'store_id' => $table['store_id']
        ];

        $res = Db::name('longbing_card_user')->where(['id'=>$user_id])->update($update);

        $user = $user_info;

        $user['store_id'] = $table['store_id'];

        $user['table_id'] = $table_id;

        $key = 'longbing_card_user_' . $user_id;

        setCache($key,$user,1800,$table['uniacid']);

        setCache($this->autograph,$user,1800,$table['uniacid']);

        return $user;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-15 13:18
     * @功能说明:小程序端的门店列表
     */
    public function indexStoreList($dis,$alh){

        $data = $this->where($dis)->field(['*',$alh])->order('distance asc,id desc')->paginate(10)->toArray();


        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function storeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-13 17:52
     * @功能说明:门店排行版
     */

    public function storeTop($uniacid,$page,$title=''){

        $dis[] = ['a.uniacid','=',$uniacid];

        if(!empty($title)){

            $dis[] = ['a.title','like','%'.$title.'%'];
        }

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_order b','a.id = b.store_id AND b.pay_type >0','left')
                ->where($dis)
                ->where('a.status','>',-1)
                ->field(['a.title','a.id','sum(b.pay_price) as order_price'])
                ->group('a.id')
                ->order('order_price desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-16 18:28
     * @功能说明:门店配置
     */
    public function storeSendType($uniacid,$type){

        $dis[] = ['a.uniacid','=',$uniacid];

        $dis[] = ['a.status','>',-1];

        $dis[] = ['b.send_type','=',$type];

        $data = $this->alias('a')
                ->join('longbing_card_restaurant_store_config b','a.id = b.store_id')
                ->where($dis)
                ->column('a.title');

        return !empty($data)?implode(',',$data):[];


    }











}